﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Pixer
{
    class Fill : Shape
    {
        private Point position;
        /// <summary>
        /// 填充的中心坐标
        /// </summary>
        public Point Position
        {
            get { return position; }
            set { position = value; }
        }


        public override void paint(System.Drawing.Graphics g)
        {
            throw new NotImplementedException();
        }

        public override void paint(System.Drawing.Bitmap b)
        {
            boundaryfill4(position.X,position.Y,FillColor,b.GetPixel(position.X,position.Y),b);
        }

        protected void boundaryfill4(int seedx, int seedy, Color fcolor, Color bcolor,Bitmap bitmap)
        {
            try
            {
                Queue<Point> pq = new Queue<Point>();
                Color color;
                Point p = new Point();
                Point pt = new Point();
                Point p1 = new Point();
                Point p2 = new Point();
                Point p3 = new Point();
                Point p4 = new Point();
                bitmap.SetPixel(seedx, seedy, fcolor);
                p.X = seedx;
                p.Y = seedy;
                pq.Enqueue(p);
                while (pq.Count > 0)
                {
                    pt = pq.Dequeue();
                    color = bitmap.GetPixel(pt.X, pt.Y - 1);
                    if (color.ToArgb() == bcolor.ToArgb() && color.ToArgb() != fcolor.ToArgb())
                    {
                        bitmap.SetPixel(pt.X, pt.Y - 1, fcolor);
                        p1.X = pt.X;
                        p1.Y = pt.Y - 1;
                        pq.Enqueue(p1);
                    }

                    color = bitmap.GetPixel(pt.X, pt.Y + 1);
                    if (color.ToArgb() == bcolor.ToArgb() && color.ToArgb() != fcolor.ToArgb())
                    {
                        bitmap.SetPixel(pt.X, pt.Y + 1, fcolor);
                        p2.X = pt.X;
                        p2.Y = pt.Y + 1;
                        pq.Enqueue(p2);
                    }

                    color = bitmap.GetPixel(pt.X - 1, pt.Y);
                    if (color.ToArgb() == bcolor.ToArgb() && color.ToArgb() != fcolor.ToArgb())
                    {
                        bitmap.SetPixel(pt.X - 1, pt.Y, fcolor);
                        p3.X = pt.X - 1;
                        p3.Y = pt.Y;
                        pq.Enqueue(p3);
                    }

                    color = bitmap.GetPixel(pt.X + 1, pt.Y);
                    if (color.ToArgb() == bcolor.ToArgb() && color.ToArgb() != fcolor.ToArgb())
                    {
                        bitmap.SetPixel(pt.X + 1, pt.Y, fcolor);
                        p4.X = pt.X + 1;
                        p4.Y = pt.Y;
                        pq.Enqueue(p4);
                    }

                }
            }
            catch { }
        }
    }
}
